package com.zr.sparesubclass.service.impl;

import com.zr.sparesubclass.bean.LegalPerson;
import com.zr.sparesubclass.bean.Plant;
import com.zr.sparesubclass.mapper.PlantMapper;
import com.zr.sparesubclass.model.AddPlantVo;
import com.zr.sparesubclass.model.PlantQueryVo;
import com.zr.sparesubclass.model.UpdateEnabledVo;
import com.zr.sparesubclass.service.PlantService;
import com.zr.util.AllRecords;
import com.zr.util.ResultVO;
import com.zr.util.ResultVOBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PlantServiceImpl implements PlantService {
    @Autowired
    private PlantMapper plantMapper;
    @Override
    public ResultVO<List<LegalPerson>> findLegalPersonByUserId(int id) {
        return ResultVOBuilder.success(plantMapper.findLegalPersonByUserId(id));
    }

    @Override
    public ResultVO<AllRecords> legalQueryVo(PlantQueryVo plantQueryVo) {
        int count =plantMapper.queryCount(plantQueryVo);
        List<Plant> plants = plantMapper.queryPage(plantQueryVo);
        AllRecords allRecords = new AllRecords();
        allRecords.setDataList(plants);
        allRecords.setPageIndex(plantQueryVo.getPageIndex());
        allRecords.setPageSize(plantQueryVo.getPageSize());
        allRecords.setTotalNumber(count);
        allRecords.resetTotalNumber(count);
//        allRecords.setTotalPage(allRecords.getTotalPage());
        return ResultVOBuilder.success(allRecords);
    }

    @Override
    public ResultVO<UpdateEnabledVo> updatestatus(UpdateEnabledVo updateEnabledVo) {
        plantMapper.updatestatus(updateEnabledVo);
        return ResultVOBuilder.success(updateEnabledVo);
    }

    @Override
    public ResultVO<AddPlantVo> add(AddPlantVo addPlantVo) {
        List<Plant> plant=plantMapper.findPlantByCode(addPlantVo.getLegalPlantCode());
        if(plant.size()>0){
            return ResultVOBuilder.error("500","工厂编码已经存在");
        }
        addPlantVo.setLegalPersonName(plantMapper.querylegalPersonName(addPlantVo.getLegalPersonCode()));
        plantMapper.add(addPlantVo);
        return ResultVOBuilder.success(addPlantVo);
    }

    @Override
    public ResultVO<AddPlantVo> update(AddPlantVo addPlantVo) {
        if(addPlantVo.getVersion()==plantMapper.findPlantByPlantCode(addPlantVo.getLegalPlantCode())){
            plantMapper.update(addPlantVo);
            return ResultVOBuilder.success(addPlantVo);
        }else{
            return  ResultVOBuilder.error("500","修改信息已改变，刷新后重试");
        }

    }


}
